package com.taptech.doufu.ui.view.topicview.adapter;

import android.content.Context;
import android.graphics.Paint;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.adapter.DfhomeNovelAdapter3;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.ui.view.topicview.ContentType;
import com.taptech.doufu.ui.view.topicview.UmengEventUtil;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicGridAdapter extends RecyclerView.Adapter<NovelTopicGridAdapter.ViewHolder>{

    private List<DFHomeNovelBeans> list;
    private Context context;
    private int picWidth;
    private int picHeight;
    private String type;
    private String pageType;
    private EventBean eventData;
    private ContentType contentType;
    public NovelTopicGridAdapter(Context context, List<DFHomeNovelBeans> list, ContentType contentType, EventBean eventData, String type,String pageType){
        this.list = list;
        this.context = context;
        this.eventData = eventData;
        this.type = type;
        this.contentType = contentType;
        this.pageType = pageType;
        picHeight = ChoiceUtils.getHeight(context);
        picWidth = ChoiceUtils.getPicWidth(context);
    }

    public DFHomeNovelBeans getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }



    public void refrush(List<DFHomeNovelBeans> list) {
        if (list != null) {
            this.list = list;
        }else {
            this.list.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.topic_novel_grid_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.title = view.findViewById(R.id.title);
        holder.ivPic = view.findViewById(R.id.ivPic);
        holder.ivNum = view.findViewById(R.id.ivNum);
        holder.ivVip = view.findViewById(R.id.ivVip);
        holder.lay = view.findViewById(R.id.lay);
        holder.tvTag1 = view.findViewById(R.id.tvTag1);
        holder.tvTag2 = view.findViewById(R.id.tvTag2);
        holder.tvAuthor = view.findViewById(R.id.tvAuthor);
        holder.layPrice = view.findViewById(R.id.layPrice);
        holder.layTag = view.findViewById(R.id.layTag);
        holder.tvTotalPrice = view.findViewById(R.id.tvTotalPrice);
        holder.tvEndPrice = view.findViewById(R.id.tvEndPrice);
        RelativeLayout.LayoutParams picParams = new RelativeLayout.LayoutParams(picWidth,picHeight);
        holder.ivPic.setLayoutParams(picParams);
        holder.title.setWidth(picWidth + ScreenUtil.dip2px(context, 4));
        return holder;
    }



    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final DFHomeNovelBeans model = getItem(position);
        if (model != null) {
            holder.title.setText(model.getTitle());
            if (TextUtils.isEmpty(model.getCover_left_top_icon_url())) {
                if ("1".equals(model.getIs_vip())) {
                    holder.ivVip.setVisibility(View.VISIBLE);
                } else {
                    holder.ivVip.setVisibility(View.GONE);
                }
                holder.ivNum.setVisibility(View.GONE);
            } else {
                holder.ivNum.setVisibility(View.VISIBLE);
                holder.ivVip.setVisibility(View.GONE);
                GlideUtil.displayImage(context, holder.ivNum, model.getCover_left_top_icon_url());
            }

            if (model.getImages() != null && model.getImages().length > 0) {
                if ((model.getImages()[0].getImgUrl() != null) && (model.getImages()[0].getImgUrl().length() > 0)) {
                    GlideUtil.displayRoundImage(context, holder.ivPic,model.getImages()[0].getImgUrl(),3,R.drawable.img_default_loading_l);
                }
            }
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengEventUtil.addContentEvent(context,eventData,type,pageType);
                    DfhomeNovelAdapter3.goToNovelDesActivity(context, String.valueOf(model.getId()),
                            model.getObject_type(), model.getTitle(), type,model.getTitle());
                }
            });

            holder.layTag.setVisibility(View.GONE);
            holder.tvAuthor.setVisibility(View.GONE);
            holder.layPrice.setVisibility(View.GONE);
            if (contentType == ContentType.PRICE) {
                holder.layPrice.setVisibility(View.VISIBLE);
                holder.tvEndPrice.setText(String.valueOf((int) model.getEnd_price()));
                holder.tvTotalPrice.setText(String.valueOf((int) model.getTotal_price()));
                holder.tvTotalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            }else{
                if ((model.getTags() != null) && (model.getTags().length > 0)) {
                    holder.layTag.setVisibility(View.VISIBLE);
                    if (model.getTags()[0] != null) {
                        holder.tvTag1.setText((model.getTags()[0]).getName());
                        holder.tvTag1.setVisibility(View.VISIBLE);
                    }else{
                        holder.tvTag1.setVisibility(View.GONE);
                    }
                    if ((model.getTags().length > 1) && (model.getTags()[1] != null)) {
                        holder.tvTag2.setText((model.getTags()[1]).getName());
                        holder.tvTag2.setVisibility(View.VISIBLE);
                    }else{
                        holder.tvTag2.setVisibility(View.GONE);
                    }
                } else if (model.getUser() != null && !TextUtils.isEmpty(model.getUser().getNickname())) {
                    holder.tvAuthor.setVisibility(View.VISIBLE);
                    holder.tvAuthor.setText(model.getUser().getNickname());
                }
                holder.tvTag1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DfhomeNovelAdapter3.goToTagActivity(context,holder.tvTag1.getText().toString(),type);
                    }
                });
                holder.tvTag2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DfhomeNovelAdapter3.goToTagActivity(context,holder.tvTag2.getText().toString(),type);
                    }
                });
            }
            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView ivPic,ivVip,ivNum;
        TextView tvTag1,tvTag2;
        View lay,layTag,layPrice;
        TextView tvEndPrice,tvTotalPrice;
        TextView tvAuthor;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_color_1));
        viewHolder.ivPic.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
    }
}
